//
//  TSBugTapOnTextViewController.m
//  CJUIKitDemo
//
//  Created by ciyouzen on 15/11/12.
//  Copyright (c) 2015年 dvlproad. All rights reserved.
//

#import "TSBugTapOnTextViewController.h"
#import <CQDemoKit/CJUIKitToastUtil.h>
#import <CQDemoKit/CQTSRipeTableView.h>
#import <CQDemoKit/CQTSButtonFactory.h>

#import "UIViewController+TSBug.h"

@interface TSBugTapOnTextViewController () <UITextViewDelegate, UIGestureRecognizerDelegate> {
    
}
@property (nonatomic, strong) UITextView *textView;

@property (nonatomic, assign, readonly) BOOL fixBug;    /**< 是否修复bug（默认否） */

@end

@implementation TSBugTapOnTextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = NSLocalizedString(@"修复无法编辑的textView没法响应tapGesture", nil);
    
    UIButton *bugStateButton = [CQTSButtonFactory bugButtonWithFixBugHandle:^{
        [self fixBug:NO];
    } reproduceBugHandle:^{
        [self fixBug:YES];
    }];
    [self.view addSubview:bugStateButton];
    [bugStateButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_topLayoutGuide).mas_offset(10);
        make.left.mas_equalTo(20);
        make.centerX.mas_equalTo(self.view);
        make.height.mas_equalTo(44);
    }];
    
    
    UILabel *knowledgeLabel = [self knowledgeLabel];
    knowledgeLabel.text = @"1、未禁用textView的userInteractionEnabled为NO时候，点击到文本框会无法响应tapGR。\n2、但通过editable = NOh和selectable = NO可达到textView响应tap";
    [self.view addSubview:knowledgeLabel];
    [knowledgeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(bugStateButton.mas_bottom).mas_offset(10);
        //make.height.mas_equalTo(160);
    }];
    
    
    UITextView *textView = [[UITextView alloc] init];
    textView.backgroundColor = [UIColor redColor];
    textView.showsVerticalScrollIndicator = YES;
    textView.textAlignment = NSTextAlignmentCenter;
    textView.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:36];
    textView.textColor = [UIColor whiteColor];
    textView.text = @"你好呀陌生人啊啊 聊会天呗你好呀陌生人啊啊 聊会天呗你好呀陌生人啊啊 聊会天呗你好呀陌生人啊啊 聊会天呗你好呀陌生人啊啊 聊会天呗你好呀陌生人啊啊 聊会天呗你好呀陌生人啊啊 聊会天呗你好呀陌生人啊啊 聊会天呗";
    textView.delegate = self;
    [self.view addSubview:textView];
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(knowledgeLabel.mas_bottom).mas_offset(10);
        make.height.mas_equalTo(100);
    }];
    self.textView = textView;
    
    
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBlankViewAction:)];
    tapGesture.delegate = self;
    [self.view addGestureRecognizer:tapGesture];
}


#pragma mark - Event
/*
 *  是否要修复bug
 *
 *  @param shouldFixBug 是否要修复bug（YES:修复bug; NO:重现bug）
 */
- (void)fixBug:(BOOL)shouldFixBug {
    _fixBug = shouldFixBug;
    
    BOOL hasFixBug = shouldFixBug; // 是否已经修复好bug，运行正常了
    if (hasFixBug) {
        self.textView.editable = NO;     // textView.editable = NO;的时候不会执行textViewShouldBeginEditing
        self.textView.selectable = NO;
    } else {
        self.textView.editable = YES;     // textView.editable = NO;的时候不会执行textViewShouldBeginEditing
        self.textView.selectable = YES;
    }
}

/** 点击空白区域的事件 */
- (void)tapBlankViewAction:(UITapGestureRecognizer *)tap {
    [self __showToast:@"响应了TapGR" centerOffset:CGPointMake(0, 100)];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [[self nextResponder] touchesBegan:touches withEvent:event];
    [super touchesBegan:touches withEvent:event];
    
    [self __showToast:@"响应了TouchBegin" centerOffset:CGPointMake(0, 40)];
}


   
#pragma mark - UITextViewDelegate
// textView.editable = NO; 的时候不会执行textViewShouldBeginEditing
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    [self __showToast:@"响应了TextView的点击" centerOffset:CGPointMake(0, -40)];
    return NO;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
